<script setup lang="js">
import {ref,onMounted} from "vue";
import {fetchProductList} from "@/api/product.js";
import {ElMessage, ElMessageBox} from "element-plus";
import {createHotProduct} from "@/api/marketingHot.js";

const emits = defineEmits(['close','refresh']);
const dialogData = ref({
  list: null,
  total: null,
  selectHot: [],
  listQuery: {
    keyword: null,
    pageNum: 1,
    pageSize: 5
  }
});
const totalHotDialog = ref(0);

onMounted(() => {
  dialogData.value.listQuery.keyword = "";
  getHotListDialog();
})
const getHotListDialog = async () => {
  const res = await fetchProductList(dialogData.value.listQuery);
  dialogData.value.list = res.data.data.list;
  totalHotDialog.value = res.data.data.total;
}

const handleDialogSelectionChange = (val) => {
  dialogData.value.selectHot = val;
}

// TODO：搜索
const handleSelectSearch = () => {
  dialogData.value.listQuery.pageNum = 1;
  getHotListDialog();
}

// TODO: 确定新增推荐专题
const handleSelectDialogConfirm = () => {
  if (dialogData.value.selectHot.length < 1) {
    dialogData.value.selectHot = [];
    ElMessage.error('请选择一条专题！');
    return;
  }
  let selectHot = []
  for (let i = 0; i < dialogData.value.selectHot.length; i++) {
    selectHot.push({
      subjectId: dialogData.value.selectHot[i].id,
      subjectName: dialogData.value.selectHot[i].title,
    });
  }
  ElMessageBox.confirm('是否确定推荐该专题？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    createHotProduct(selectHot).then(res => {
      ElMessage.warning(res.data.message)
      ElMessage.success('推荐专题成功！');
      emits('refresh');
      emits('close');
      getHotListDialog();
    }).catch(err => {
      ElMessage.error(err.data.message);
      ElMessage.error('推荐专题失败！');
    });
  }).catch(() => {
    emits('close');
    ElMessage.info('已取消操作！');
  });
  getHotListDialog();
}

// TODO: 关闭弹窗
const handleCloseDialog = () => {
  emits('close');
}

// TODO: 确定新增推荐专题--分页操作
const handleDialogSizeChange = (size) => {
  dialogData.value.listQuery.pageSize = size;
  getHotListDialog();
}
const handleDialogCurrentChange = (val) => {
  dialogData.value.listQuery.pageNum = val;
  getHotListDialog();
}
</script>

<template>
  <el-dialog
    title="添加人气推荐">
    <el-input v-model="dialogData.listQuery.keyword"
              style="width: 300px;margin-bottom: 20px"
              placeholder="专题名称搜索"
              clearable
    >
      <template #append>
        <el-button icon="search" @click="handleSelectSearch()"></el-button>
      </template>
    </el-input>
    <el-table :data="dialogData.list"
              @selection-change="handleDialogSelectionChange" border>
      <el-table-column type="selection" width="60" align="center"></el-table-column>
      <el-table-column label="商品名称" align="center">
        <template #default="{row}">{{ row.name }}</template>
      </el-table-column>
      <el-table-column label="货号" width="200" align="center">
        <template #default="{row}">NO.{{ row.productSn }}</template>
      </el-table-column>
      <el-table-column label="价格" width="200" align="center">
        <template #default="{row}">￥{{ row.price }}</template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
          background
          layout="total, sizes,prev, pager, next,jumper"
          :total="totalHotDialog"
          :page-sizes="[5,10,15,20]"
          v-model:page-size="dialogData.listQuery.pageSize"
          v-model:current-page="dialogData.listQuery.pageNum"
          @size-change="handleDialogSizeChange"
          @current-change="handleDialogCurrentChange"
      >
      </el-pagination>
    </div>
    <div class="dialog-footer">
      <el-button @click="handleCloseDialog()">取 消</el-button>
      <el-button type="primary" @click="handleSelectDialogConfirm()">确 定</el-button>
    </div>
  </el-dialog>
</template>

<style scoped Lang="scss">
.pagination-container,
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  margin: 20px 0;
}
</style>